package xxm.view.crawler;

import java.util.ArrayList;
import java.util.List;

import xxm.util.BasicDefine;
import xxm.util.BasicParserProperties;
import xxm.utility.parser.BasicBlogParserImpl;
import xxm.utility.thread.BasicThreadPoolConfig;
import xxm.utility.thread.BasicThreadPoolService;

public class CrawlerManager {

	//private BasicBlogParserImpl parser = new BasicBlogParserImpl();
	
	public void initCrawler() {
		/*if( null == parser ) {
			return;
		}
		
		parser.setPapserURL(BasicParpserProperties.readValue("papser_url"));
		parser.setStartPage(Integer.valueOf(BasicParpserProperties.readValue("start_page")));
		parser.setTag(BasicParpserProperties.readValue("papser_tag"));
		parser.setEncoding(BasicParpserProperties.readValue("encode"));
		parser.setMaxPage(Integer.valueOf(BasicParpserProperties.readValue("MAX_PAGE")));*/
		
		
		BasicThreadPoolConfig config1 = new BasicThreadPoolConfig();
		config1.setPoolName("basic_thread1");
		BasicThreadPoolConfig config2 = new BasicThreadPoolConfig();
		config2.setPoolName("basic_thread2");
		BasicThreadPoolConfig config3 = new BasicThreadPoolConfig();
		config3.setPoolName("basic_thread3");
		
		List<BasicThreadPoolConfig> list = new ArrayList<BasicThreadPoolConfig>();
		list.add(config1);
		list.add(config2);
		list.add(config3);
		
		BasicThreadPoolService.getInstance().setConfigPools(list);
		BasicThreadPoolService.getInstance().init();
		
		int nLen = Integer.valueOf(BasicParserProperties.readValue(BasicDefine.target_max_page_def));
		int nIndex = Integer.valueOf(BasicParserProperties.readValue(BasicDefine.target_start_page_def));
		while( nIndex < nLen ) {
			PapserThread TaskThread = new PapserThread(nIndex);
			
			nIndex+=10;
		}
	}
	
	class PapserThread  implements Runnable {

		private int nPage;
		
		public PapserThread(int nPage) {
			this.nPage = nPage;
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			BasicBlogParserImpl papser = new BasicBlogParserImpl();
			papser.setPapserURL(BasicParserProperties.readValue(BasicDefine.target_domain_def));
			//papser.setStartPage(Integer.valueOf(BasicParpserProperties.readValue("start_page")));
			papser.setStartPage(nPage);
			papser.setTag(BasicParserProperties.readValue(BasicDefine.target_papser_tag_def));
			papser.setEncoding(BasicParserProperties.readValue(BasicDefine.target_encode_def));
			//papser.setMaxPage(Integer.valueOf(BasicParpserProperties.readValue("MAX_PAGE")));
			papser.setMaxPage(nPage+10);
			
			//papser.parserAttribute(nPage);
			papser.ParserAttribute();
		}
		
	}
}
